<?php

namespace app\data\controller\user;

use app\data\model\DataUser;
use app\data\model\DataUserTransfer;
use app\data\service\UserAdminService;
use app\data\service\UserTransferService;
use think\admin\Controller;
use think\admin\extend\CodeExtend;
use think\admin\service\AdminService;

/**
 * 用户提现管理
 * Class Transfer
 * @package app\data\controller\user
 */
class Transfer extends Controller
{
    /**
     * 提现转账方案
     * @var array
     */
    protected $types = [];

    protected function initialize()
    {
        $this->types = UserTransferService::instance()->types();
    }


    /**
     * 配置数据处理
     * @param string $tpl 模板文件
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function _sysdata(string $tpl = '')
    {
        if ($this->request->isGet()) {
            $this->data = sysdata($this->skey);
            $this->fetch($tpl);
        } else {
            sysdata($this->skey, $this->request->post());
            $this->success('配置修改成功');
        }
    }

    /**
     * 用户提现管理
     * @menu true
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = '用户提现管理';
        $this->transfer = UserTransferService::amount(0);
        // 创建查询对象
        $query = DataUserTransfer::mQuery()->order('id desc');
        // 用户条件搜索
        $db = DataUser::mQuery()->like('phone,username|nickname#nickname')->db();
        if ($db->getOptions('where')) $query->whereRaw("uuid in {$db->field('id')->buildSql()}");
        // 数据列表处理
        $lists = $query->equal('audit_status')->dateBetween('create_at')->page(true, false, false);
        if(empty($lists['list']) === FALSE){
            foreach ($lists['list'] as &$lv){
                $lv['amount'] = sprintf("%.2f",$lv['amount']/100);
                $lv['charge_amount'] = sprintf("%.2f",$lv['charge_amount']/100);
            }
        }
        $this->list = $lists['list'];
        $this->fetch();
    }

    /**
     * 数据列表处理
     * @param array $data
     */
    protected function _page_filter(array &$data)
    {
        UserAdminService::buildByUid($data);
    }

    /**
     * 提现审核操作
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function auditStatus()
    {
        $this->_audit();
    }

    /**
     * 标记打款操作
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function auditPayment()
    {
        $this->_marking();
    }

    /**
     * 提现审核打款
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function _audit()
    {
        if ($this->request->isGet()) {
            $this->from = input('from',0);//1  明细  0  审核
            DataUserTransfer::mForm('audit', 'code');
        } else {
            $data = $this->_vali([
                'code.require'        => '打款单号不能为空！',
                'audit_status.require'      => '交易审核操作类型！',
                'audit_status.in:0,1,2,3' => '交易审核操作类型！',
                'audit_remark.default'      => '',
                'type.default'  => 1,
            ]);
            $map = ['code' => $data['code']];
            $find = DataUserTransfer::mk()->where($map)->find();
            if (empty($find)) $this->error('不允许操作审核！');
            // 提现状态(0 审核中   1  待打款   2  已打款  3 驳回)

            $data['trade_time'] = date('Y-m-d H:i:s');
            $data['change_time'] = date('Y-m-d H:i:s');
            $data['audit_datetime'] = date('Y-m-d H:i:s');
            if($data['type'] == 1){
                //审核通过
                $data['audit_status'] = 1;
                $data['audit_remark'] = ($data['audit_remark'] ?: '审核成功') . ' By ' . AdminService::getUserName();
                sysoplog('通过提现审核', $data['audit_remark']);
            }else{
                //驳回审核
                $data['audit_status'] = 3;
                $data['audit_remark'] = ($data['audit_remark'] ?: '驳回审核') . ' By ' . AdminService::getUserName();
                sysoplog('驳回提现审核', $data['audit_remark']);
            }
            $res = DataUserTransfer::mk()->strict(false)->where($map)->update($data);
            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败！');
            }
        }
    }
    /**
     * 标记打款
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function _marking()
    {
        if ($this->request->isGet()) {
            DataUserTransfer::mForm('marking', 'code');
        } else {
            $data = $this->_vali([
                'code.require'        => '打款单号不能为空！',
                'path.require'  => '打款凭证不能为空',
            ]);
            $map = ['code' => $data['code']];
            $find = DataUserTransfer::mk()->where($map)->find();
            if (empty($find)) $this->error('不允许操作审核！');

            $data['change_time'] = date('Y-m-d H:i:s');
            $data['trade_time'] = date('Y-m-d H:i:s');
            $data['audit_status'] = 2;

            sysoplog('上传打款凭证',AdminService::getUserName().'上传打款凭证，流水号：'.$data['code']);
            $res = DataUserTransfer::mk()->strict(false)->where($map)->update($data);
            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败！');
            }
        }
    }

}
